from django.db import models

from django.contrib.auth import get_user_model
# 根据setting中的AUTH_USER_MODEL得到userprofile在哪个class;这是另一种方法，可以直接用apps导入包
# 返回用户模型
from apps.courses.models import Course
from apps.users.models import BaseModel

# 提交个人信息咨询
# 评论
# 收藏
# 消息
# 用户课程关系

UserProfile = get_user_model()
# 使用get_user_model方法，得到有没有覆盖django默认的user


class Banner(BaseModel):
    """轮播图"""
    title = models.CharField(max_length=100, verbose_name="标题")
    image = models.ImageField(upload_to="banner/%Y/%m", max_length=200, verbose_name="轮播图")
    url = models.URLField(max_length=1000, verbose_name="访问地址")
    index = models.IntegerField(default=0, verbose_name="顺序")

    class Meta:
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class UserAsk(BaseModel):
    """咨询，提交姓名电话课程名，不需要登录"""
    name = models.CharField(max_length=20, verbose_name="姓名")
    mobile = models.CharField(max_length=11, verbose_name="手机")
    course_name = models.CharField(max_length=50, verbose_name="课程名")

    def __str__(self):
        return f"{self.name}_{self.course_name}({self.mobile})"

    class Meta:
        verbose_name = "用户咨询"
        verbose_name_plural = verbose_name
        db_table = "tb_userask"


class CourseComments(BaseModel):
    """用户评论，必须登录后才能评论"""
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    comments = models.CharField(max_length=200, verbose_name="评论内容")

    def __str__(self):
        return self.comments

    class Meta:
        verbose_name = "课程评论"
        verbose_name_plural = verbose_name
        db_table = "tb_comments"


class UserFavorite(BaseModel):
    """用户收藏"""
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
    fav_id = models.IntegerField(verbose_name="数据id")
    fav_type = models.IntegerField(choices=((1, "课程"), (2, "课程机构"), (3, "讲师")), default=1, verbose_name="收藏类型")

    def __str__(self):
        return f"{self.user.username}_{self.fav_id}"

    class Meta:
        verbose_name = "用户收藏"
        verbose_name_plural = verbose_name
        db_table = "tb_favorite"


class UserMessage(BaseModel):
    """用户消息"""
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
    message = models.CharField(max_length=200, verbose_name="消息内容")
    has_read = models.BooleanField(default=False, verbose_name="是否已读")

    class Meta:
        verbose_name = "用户消息"
        verbose_name_plural = verbose_name
        db_table = "tb_message"

    def __str__(self):
        return self.message


class UserCourse(BaseModel):
    """用户课程表(多对多后期修改）"""
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")

    def __str__(self):
        return self.course.name

    class Meta:
        verbose_name = "用户课程"
        verbose_name_plural = verbose_name
        db_table = "tb_usercourse"
